package com.inhim.pjhim.activity;

import android.content.Intent;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inhim.pjhim.R;
import com.inhim.pjhim.app.BaseActivity;
import com.inhim.pjhim.entity.HelpInfoEntity;
import com.inhim.pjhim.http.MyOkHttpClient;
import com.inhim.pjhim.http.Urls;
import com.inhim.pjhim.view.BToast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Request;

public class IssueInfoActivity extends BaseActivity {
    private Gson gson = new Gson();
    private TextView iv_answer;
    @Override
    public Object offerLayout() {
        return R.layout.activity_issue_info;
    }

    @Override
    public void onBindView() {
        Intent intent=getIntent();
        String Query=intent.getStringExtra("Query");
        int CommonProblemId=intent.getIntExtra("CommonProblemId",0);
        ImageView iv_back=findViewById(R.id.iv_back);
        iv_back.setOnClickListener(v -> finish());
        TextView iv_title=findViewById(R.id.tvCourse);
        iv_answer=findViewById(R.id.iv_answer);
        if(Query!=null){
            iv_title.setText(Query);
        }
        getHelpInfo(CommonProblemId);
    }

    @Override
    public void destory() {

    }

    private void getHelpInfo(int CommonProblemId) {
        showLoading("加载中");
        MyOkHttpClient.getInstance().asyncGet(Urls.getHelpInfo(CommonProblemId), new MyOkHttpClient.HttpCallBack() {
            @Override
            public void onError(Request request, IOException e) {
                hideLoading();
                BToast.showText("请求失败", false);
            }

            @Override
            public void onSuccess(Request request, String result) {
                hideLoading();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.getInt("code") == 0) {
                        HelpInfoEntity helpListEntity = gson.fromJson(result, HelpInfoEntity.class);
                        if(helpListEntity.getCommonProblem().getAnswer()!=null){
                            iv_answer.setText(helpListEntity.getCommonProblem().getAnswer());
                        }
                    } else {
                        BToast.showText(jsonObject.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}
